package com.xaicode.learn.netty.heartbeat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * @author Locker xaicode@sina.com
 * @version 1.0
 * @date 2021/6/19 0019 21:21
 */
public class HeartBeatServer {

    public static void main(String[] args) {

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap
                    .group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();

                            /*
                                readerIdleTime – an IdleStateEvent whose state is IdleState.READER_IDLE will be triggered
                                    when no read was performed for the specified period of time. Specify 0 to disable.
                                writerIdleTime – an IdleStateEvent whose state is IdleState.WRITER_IDLE will be triggered
                                    when no write was performed for the specified period of time. Specify 0 to disable.
                                allIdleTime – an IdleStateEvent whose state is IdleState.ALL_IDLE will be triggered
                                    when neither read nor write was performed for the specified period of time. Specify 0 to disable.
                                unit – the TimeUnit of readerIdleTime, writeIdleTime, and allIdleTime.
                             */

                            // Triggers an IdleStateEvent when a Channel has not performed read, write, or both operation for a while.
                            // this handler will fire next handler#userEventTriggered
                            pipeline.addLast(new IdleStateHandler(3, 4, 5, TimeUnit.SECONDS));
                            // add self IdleStateHandler
                            pipeline.addLast(new HeartBeatServerHandler());
                        }
                    });

            ChannelFuture channelFuture = serverBootstrap.bind(7777).sync();

            channelFuture.channel().closeFuture().sync();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
